﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RsaRegister.Model
{
    public  class ProductVersion
    {
        public int FirstField { get; set; }
        public int SecondField { get; set; }
        public int ThirdField { get; set; }
        public int FouthField { get; set; }
        public ProductVersion() { }
        public ProductVersion(string version)
        {
            string[] strs = version.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if(strs.Length==4)
            {
                if(int.TryParse(strs[0],out int first)&&int.TryParse(strs[1],out int second)&&int.TryParse(strs[2],out int third)&&int.TryParse(strs[3],out int fouth))
                {
                    FirstField = first;
                    SecondField = second;
                    ThirdField = third;
                    FouthField = fouth;
                }
            }
        }
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return $"{FirstField}.{SecondField}.{ThirdField}.{FouthField}";
        }
        public static bool operator >(ProductVersion ver1, ProductVersion ver2)
        {
            if (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField && ver1.ThirdField == ver2.ThirdField) return ver1.FouthField > ver2.FouthField;
            if (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField) return ver1.ThirdField > ver2.ThirdField;
            if (ver1.FirstField == ver2.FirstField ) return ver1.SecondField > ver2.SecondField;
            return ver1.FirstField > ver2.FirstField;
        }
        public static bool operator <(ProductVersion ver1, ProductVersion ver2)
        {
            if (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField && ver1.ThirdField == ver2.ThirdField) return ver1.FouthField < ver2.FouthField;
            if (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField) return ver1.ThirdField < ver2.ThirdField;
            if (ver1.FirstField == ver2.FirstField) return ver1.SecondField < ver2.SecondField;
            return ver1.FirstField < ver2.FirstField;
        }
        public static bool operator >=(ProductVersion ver1, ProductVersion ver2)
        {
            if (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField && ver1.ThirdField == ver2.ThirdField) return ver1.FouthField >= ver2.FouthField;
            if (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField) return ver1.ThirdField >= ver2.ThirdField;
            if (ver1.FirstField == ver2.FirstField) return ver1.SecondField >= ver2.SecondField;
            return ver1.FirstField >= ver2.FirstField;
        }
        public static bool operator <=(ProductVersion ver1, ProductVersion ver2)
        {
            if (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField && ver1.ThirdField == ver2.ThirdField) return ver1.FouthField <= ver2.FouthField;
            if (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField) return ver1.ThirdField <= ver2.ThirdField;
            if (ver1.FirstField == ver2.FirstField) return ver1.SecondField <= ver2.SecondField;
            return ver1.FirstField <= ver2.FirstField;
        }
        public static bool operator ==(ProductVersion ver1, ProductVersion ver2)
        {
            return (ver1.FirstField == ver2.FirstField && ver1.SecondField == ver2.SecondField && ver1.ThirdField == ver2.ThirdField && ver1.FouthField == ver2.FouthField);
        }
        public static bool operator !=(ProductVersion ver1, ProductVersion ver2)
        {
            return (ver1.FirstField != ver2.FirstField || ver1.SecondField != ver2.SecondField || ver1.ThirdField != ver2.ThirdField || ver1.FouthField != ver2.FouthField);
        }
    }
}
